package com.sptpc.exam;

import com.sptpc.exam.model.User;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/usermanage.do")
public class UserMangeServlet extends HttpServlet {
    // 保存所有账号
    private final List<User> users = new ArrayList<>();

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        // 加载账号信息
        User user1 =new User(1,"test01","张三","软件技术");
        User user2 =new User(2,"test02","李四","通信技术");
        User user3 =new User(3,"test03","王五","软件技术");
        users.add(user1);
        users.add(user2);
        users.add(user3);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().setAttribute("users", users);
        request.getRequestDispatcher("userlist.jsp").forward(request, response);
    }
}
